package com.prs.crm.action.official;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.official.DeptMail;
import com.prs.crm.domain.official.DeptMailAutograph;
import com.prs.crm.domain.official.DeptMailComment;
import com.prs.crm.domain.official.DeptMailDel;
import com.prs.crm.domain.official.DeptMailNum;
import com.prs.crm.domain.official.DeptMailReceive;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.official.DeptMailAutographService;
import com.prs.crm.service.official.DeptMailCommentService;
import com.prs.crm.service.official.DeptMailDelService;
import com.prs.crm.service.official.DeptMailNumService;
import com.prs.crm.service.official.DeptMailReceiveService;
import com.prs.crm.service.official.DeptMailService;
import com.prs.crm.tools.ControlJMail;

public class DeptMailAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private File upload;
	private String uploadFileName;
	private PersonService personService;
	private String receivePersonId;
	private String types;
	private DeptMail deptMail;
	private DeptMailService deptMailService;
	private DeptMailReceive deptMailReceive;
	private DeptMailReceiveService deptMailReceiveService; 
	private List<DeptMailNum> deptMailNum;
	private DeptMailNumService deptMailNumService;
	private List<DeptMailAutograph> deptMailAutograph;
	private DeptMailAutographService deptMailAutographService;
	public List<DeptMailComment> comment;
	public DeptMailComment deptMailComment;
	public DeptMailCommentService deptMailCommentService;
	private Integer deptMailId;
	private DeptMailAutograph autograph;
	private ControlJMail cj=new ControlJMail();
	private Integer[] receiveIds;
	private DeptMailDel deptMailDel;
	private DeptMailDelService deptMailDelService;
	private Integer[] delIds;
	public DeptMailDel getDeptMailDel() {
		return deptMailDel;
	}
	public void setDeptMailDel(DeptMailDel deptMailDel) {
		this.deptMailDel = deptMailDel;
	}
	public Integer[] getDelIds() {
		return delIds;
	}
	public void setDelIds(Integer[] delIds) {
		this.delIds = delIds;
	}
	public DeptMailDelService getDeptMailDelService() {
		return deptMailDelService;
	}
	public void setDeptMailDelService(DeptMailDelService deptMailDelService) {
		this.deptMailDelService = deptMailDelService;
	}
	public Integer[] getReceiveIds() {
		return receiveIds;
	}
	public void setReceiveIds(Integer[] receiveIds) {
		this.receiveIds = receiveIds;
	}
	public ControlJMail getCj() {
		return cj;
	}
	public void setCj(ControlJMail cj) {
		this.cj = cj;
	}
	public DeptMailAutograph getAutograph() {
		return autograph;
	}
	public void setAutograph(DeptMailAutograph autograph) {
		this.autograph = autograph;
	}
	public Integer getDeptMailId() {
		return deptMailId;
	}
	public void setDeptMailId(Integer deptMailId) {
		this.deptMailId = deptMailId;
	}
	public DeptMailComment getDeptMailComment() {
		return deptMailComment;
	}
	public void setDeptMailComment(DeptMailComment deptMailComment) {
		this.deptMailComment = deptMailComment;
	}
	public DeptMailCommentService getDeptMailCommentService() {
		return deptMailCommentService;
	}
	public void setDeptMailCommentService(
			DeptMailCommentService deptMailCommentService) {
		this.deptMailCommentService = deptMailCommentService;
	}
	public List<DeptMailComment> getComment() {
		return comment;
	}
	public void setComment(List<DeptMailComment> comment) {
		this.comment = comment;
	}
	public List<DeptMailAutograph> getDeptMailAutograph() {
		return deptMailAutograph;
	}
	public void setDeptMailAutograph(List<DeptMailAutograph> deptMailAutograph) {
		this.deptMailAutograph = deptMailAutograph;
	}
	public DeptMailAutographService getDeptMailAutographService() {
		return deptMailAutographService;
	}
	public void setDeptMailAutographService(
			DeptMailAutographService deptMailAutographService) {
		this.deptMailAutographService = deptMailAutographService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public String getReceivePersonId() {
		return receivePersonId;
	}
	public void setReceivePersonId(String receivePersonId) {
		this.receivePersonId = receivePersonId;
	}
	public String getTypes() {
		return types;
	}
	public void setTypes(String types) {
		this.types = types;
	}
	public DeptMail getDeptMail() {
		return deptMail;
	}
	public void setDeptMail(DeptMail deptMail) {
		this.deptMail = deptMail;
	}
	public DeptMailService getDeptMailService() {
		return deptMailService;
	}
	public void setDeptMailService(DeptMailService deptMailService) {
		this.deptMailService = deptMailService;
	}
	public DeptMailReceive getDeptMailReceive() {
		return deptMailReceive;
	}
	public void setDeptMailReceive(DeptMailReceive deptMailReceive) {
		this.deptMailReceive = deptMailReceive;
	}
	public DeptMailReceiveService getDeptMailReceiveService() {
		return deptMailReceiveService;
	}
	public void setDeptMailReceiveService(
			DeptMailReceiveService deptMailReceiveService) {
		this.deptMailReceiveService = deptMailReceiveService;
	}
	public List<DeptMailNum> getDeptMailNum() {
		return deptMailNum;
	}
	public void setDeptMailNum(List<DeptMailNum> deptMailNum) {
		this.deptMailNum = deptMailNum;
	}
	public DeptMailNumService getDeptMailNumService() {
		return deptMailNumService;
	}
	public void setDeptMailNumService(DeptMailNumService deptMailNumService) {
		this.deptMailNumService = deptMailNumService;
	}
	@Action("listSendDeptMail")
	public String listSendDeptMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DeptMail.class);
		criteria.add(Restrictions.eq("type", 1)).add(Restrictions.eq("recorder.id",  AppContext.getAccountContext().getUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDeptMailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listReceiveDeptMail")
	public String listReceiveDeptMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DeptMailReceive.class);
		criteria.add(Restrictions.eq("type", 1)).add(Restrictions.eq("receivePerson.id", AppContext.getAccountContext().getUser().getId()));	
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDeptMailReceiveService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listRemoveDeptMail")
	public String listRemoveDeptMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DeptMailDel.class);
//		criteria.add(Restrictions.eq("receivePerson.id", AppContext.getAccountContext().getUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDeptMailDelService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("listDraftDeptMail")
	public String listDraftDeptMail(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DeptMail.class);
		criteria.add(Restrictions.eq("type",2)).add(Restrictions.eq("recorder.id",AppContext.getAccountContext().getUser().getId()));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDeptMailService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("viewDeptMail")
	public String viewDeptMail(){
		this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
		deptMail.setLastPerson(AppContext.getAccountContext().currentUser().getResume().getName());
		deptMail.setLastTime(new Date());
		this.getDeptMailService().submit(deptMail);
		return SUCCESS;
	}
	@Action("editDeptMail")
	public String editDeptMail(){
		if (delIds!=null) {
				this.deptMailDel=this.getDeptMailDelService().get(delIds[0]);
				this.deptMail=deptMailDel.getDeptMail();
				this.deptMailReceive=deptMailDel.getDeptMailReceive();
		}
		if(ids!=null){
			this.deptMail=this.getDeptMailService().get(ids[0]);
		}
		if (receiveIds!=null) {
			this.deptMailReceive=this.getDeptMailReceiveService().get(receiveIds[0]);
			this.deptMail=this.getDeptMailService().getDeptMailById(deptMailReceive.getDeptMailId());
		}
		this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
		this.deptMailAutograph=this.getDeptMailAutographService().getAllDeptMailAutograph();
		return SUCCESS;
	}
	@Action(value = "sendDeptMail", results = {
			@Result(name = "success", type = "redirect", location = "editDeptMail"),
			@Result(name = "error", type = "chain", location = "editDeptMail") })
	public String sendDeptMail(){
		if(upload!=null){
			this.deptMail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if (deptMail.getId()!=null) {
			if (deptMail.getType()==1) {
				this.addActionError("该邮件已经发送不需要再次发送");
				return ERROR;
			}
		}
		deptMail.setCommentNum(0);
		deptMail.setType(1);
		deptMail.setUpdateTime(new Date());
		deptMail.setReceivePerosnId(receivePersonId);
		this.getDeptMailService().save(deptMail);
		Collection<Person> acceptPerson=this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(deptMail.getReceivePerosnId().split(",")));
		Iterator<Person> iter=acceptPerson.iterator();
		while (iter.hasNext()) {
			Person person =iter.next();
			DeptMailReceive dr=new DeptMailReceive();
			dr.setDeptMailId(deptMail.getId());
			dr.setTitle(deptMail.getTitle());
			dr.setContent(deptMail.getContent());
			dr.setMailType(deptMail.getMailType());
			if (deptMail.getAttach()!=null) {
				dr.setAttach(deptMail.getAttach());
			}
			dr.setType(1);
			dr.setSendPerson(deptMail.getDeptMailNum().getName());
			dr.setSendPersonAdress(deptMail.getDeptMailNum().getMailAdress());
			dr.setChaoPerson(deptMail.getCopyToPerson());
			dr.setReceivePerson(person);
			dr.setState("未读");
			dr.setCommentNum(0);
			this.getDeptMailReceiveService().save(dr);
			Person per=	this.getPersonService().get(dr.getReceivePerson().getId());
			Person curper=AppContext.getAccountContext().getUser();
			/**
	    	 * 发送邮件
	    	 * 
	    	 * @param from
	    	 *            邮件发送人的邮件地址
	    	 * @param to
	    	 *            邮件接收人的邮件地址
	    	 * @param username
	    	 *            发件人的邮件帐户
	    	 * @param password
	    	 *            发件人的邮件密码
	    	 * @param object
	    	 *            邮件标题
	    	 * @param content
	    	 *            邮件内容
	    	 */
			System.out.println(curper.getResume().getEmail());
			System.out.println(per.getResume().getEmail());
			cj.sendMail(curper.getResume().getEmail(), per.getResume().getEmail(), "admin", "admin", dr.getTitle(), dr.getContent());
		}
		return SUCCESS;
	}
	@Action(value = "sendDeptMails", results = {
			@Result(name = "success", type = "redirect", location = "listReceiveDeptMail"),
			@Result(name = "error", type = "chain", location = "editForwardDeptMail") })
	public String sendDeptMails(){
		if(upload!=null){
			this.deptMail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		deptMail.setCommentNum(0);
		deptMail.setType(1);
		deptMail.setUpdateTime(new Date());
		deptMail.setReceivePerosnId(receivePersonId);
		this.getDeptMailService().save(deptMail);
		Collection<Person> acceptPerson=this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(deptMail.getReceivePerosnId().split(",")));
		Iterator<Person> iter=acceptPerson.iterator();
		while (iter.hasNext()) {
			Person person =iter.next();
			DeptMailReceive dr=new DeptMailReceive();
			dr.setDeptMailId(deptMail.getId());
			dr.setTitle(deptMail.getTitle());
			dr.setContent(deptMail.getContent());
			dr.setMailType(deptMail.getMailType());
			if (deptMail.getAttach()!=null) {
				dr.setAttach(deptMail.getAttach());
			}
			dr.setCommentNum(0);
			dr.setType(1);
			dr.setSendPerson(deptMail.getDeptMailNum().getName());
			dr.setSendPersonAdress(deptMail.getDeptMailNum().getMailAdress());
			dr.setChaoPerson(deptMail.getCopyToPerson());
			dr.setReceivePerson(person);
			dr.setState("未读");
			this.getDeptMailReceiveService().save(dr);
			Person per=	this.getPersonService().get(dr.getReceivePerson().getId());
			Person curper=AppContext.getAccountContext().getUser();
			/**
	    	 * 发送邮件
	    	 * 
	    	 * @param from
	    	 *            邮件发送人的邮件地址
	    	 * @param to
	    	 *            邮件接收人的邮件地址
	    	 * @param username
	    	 *            发件人的邮件帐户
	    	 * @param password
	    	 *            发件人的邮件密码
	    	 * @param object
	    	 *            邮件标题
	    	 * @param content
	    	 *            邮件内容
	    	 */
			System.out.println(curper.getEmail());
			System.out.println(per.getEmail());
			cj.sendMail(curper.getResume().getEmail(), per.getResume().getEmail(), "admin", "admin", dr.getTitle(), dr.getContent());
		}
		
		return SUCCESS;
	}
	@Action(value = "saveDraftDeptMail", results = {
			@Result(name = "success", type = "redirect", location = "editDeptMail"),
			@Result(name = "error", type = "chain", location = "editDeptMail") })
	public String saveDraftDeptMail(){
		if(upload!=null){
			this.deptMail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		deptMail.setCommentNum(0);
		deptMail.setType(2);
		deptMail.setUpdateTime(new Date());
		this.getDeptMailService().save(deptMail);
		return SUCCESS;
	}
	@Action("viewDeptMailReceive")
	public String viewFileTransmitReceive() {
		if(this.ids!=null){
			this.deptMailReceive=this.getDeptMailReceiveService().get(ids[0]);
		}
		if(deptMailReceive.getState().equals("未读")){
			deptMailReceive.setState("已读");
		}
		deptMailReceive.setLastPerson(AppContext.getAccountContext().currentUser().getResume().getName());
		deptMailReceive.setLastTime(new Date());
		this.getDeptMailReceiveService().submit(deptMailReceive);
		return SUCCESS;
	}
	@Action("editForwardDeptMail")
	public String editForwardDeptMail(){
		if (delIds!=null) {
			this.deptMailDel=this.getDeptMailDelService().get(delIds[0]);
			this.deptMail=deptMailDel.getDeptMail();
			this.deptMailReceive=deptMailDel.getDeptMailReceive();
	}
		if (deptMail!=null) {
			this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
			this.deptMailAutograph=this.getDeptMailAutographService().getAllDeptMailAutograph();
			deptMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
					"To:"+deptMail.getReceivePerson()+"<BR>"+
					"Sent:"+deptMail.getRecordTime()+"<BR>"+
					"Subject:"+deptMail.getTitle()+"<BR>"+
					deptMail.getContent());
			deptMail.setTitle("FW:"+deptMail.getTitle());
		}
		if (deptMailReceive!=null) {
			deptMail=new DeptMail();
			this.deptMailAutograph=this.getDeptMailAutographService().getAllDeptMailAutograph();
			this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
			deptMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+deptMailReceive.getSendPersonAdress()+";<BR>"+
					"To:"+deptMailReceive.getReceivePerson()+"<BR>"+
					"Sent:"+deptMailReceive.getRecordTime()+"<BR>"+
					"Subject:"+deptMailReceive.getTitle()+"<BR>"+
					deptMailReceive.getContent());
			deptMail.setTitle("FW:"+deptMailReceive.getTitle());
		}
		if (ids!=null) {
			this.deptMail=this.getDeptMailService().get(ids[0]);
			this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
			this.deptMailAutograph=this.getDeptMailAutographService().getAllDeptMailAutograph();
			deptMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
					"To:"+deptMail.getReceivePerson()+"<BR>"+
					"Sent:"+deptMail.getRecordTime()+"<BR>"+
					"Subject:"+deptMail.getTitle()+"<BR>"+
					deptMail.getContent());
			deptMail.setTitle("FW:"+deptMail.getTitle());
		}
		if (receiveIds!=null) {
			this.deptMailReceive=this.getDeptMailReceiveService().get(receiveIds[0]);
			deptMail=new DeptMail();
			this.deptMailAutograph=this.getDeptMailAutographService().getAllDeptMailAutograph();
			this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
			deptMail.setContent("<BR>-----Original Message -----<BR>"+
					"Form:"+deptMailReceive.getSendPersonAdress()+";<BR>"+
					"To:"+deptMailReceive.getReceivePerson()+"<BR>"+
					"Sent:"+deptMailReceive.getRecordTime()+"<BR>"+
					"Subject:"+deptMailReceive.getTitle()+"<BR>"+
					deptMailReceive.getContent());
			deptMail.setTitle("FW:"+deptMailReceive.getTitle());
		}
		return SUCCESS;
	}
	@Action("editReplyDeptMail")
	public String editReplyDeptMail(){
		this.deptMailNum=this.getDeptMailNumService().getAllDeptMailNum();
		this.deptMailAutograph=this.getDeptMailAutographService().getAllDeptMailAutograph();
		if (deptMail!=null) {
			if (types.equals("1")) {
				deptMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
						"To:"+deptMail.getReceivePerson()+"<BR>"+
						"Sent:"+deptMail.getRecordTime()+"<BR>"+
						"Subject:"+deptMail.getTitle()+"<BR>"+
						deptMail.getContent());
				deptMail.setTitle("Fe:"+deptMail.getTitle());
				this.receivePersonId=deptMail.getReceivePerosnId();
			}else{
				String[] receivePerson=this.deptMail.getReceivePerson().split(",");
				String[] receiveId=this.deptMail.getReceivePerosnId().split(",");
				deptMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
						"To:"+receivePerson[0]+"<BR>"+
						"Sent:"+deptMail.getRecordTime()+"<BR>"+
						"Subject:"+deptMail.getTitle()+"<BR>"+
						deptMail.getContent());
				deptMail.setTitle("Fe:"+deptMail.getTitle());
				this.receivePersonId=receiveId[0];
			}
		}
		if (deptMailReceive!=null) {
			DeptMail dm=this.getDeptMailService().getDeptMailById(deptMailReceive.getDeptMailId());
			if (dm!=null) {
				if (types.equals("1")) {
					deptMail=new DeptMail();
					deptMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+dm.getDeptMailNum().getMailAdress()+";<BR>"+
							"To:"+dm.getReceivePerson()+"<BR>"+
							"Sent:"+dm.getRecordTime()+"<BR>"+
							"Subject:"+dm.getTitle()+"<BR>"+
							dm.getContent());
					deptMail.setTitle("Fe:"+dm.getTitle());
					this.receivePersonId=dm.getReceivePerosnId();
				}else{
					String[] receivePerson=dm.getReceivePerson().split(",");
					String[] receiveId=dm.getReceivePerosnId().split(",");
					deptMail=new DeptMail();
					deptMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+dm.getDeptMailNum().getMailAdress()+";<BR>"+
							"To:"+receivePerson[0]+"<BR>"+
							"Sent:"+dm.getRecordTime()+"<BR>"+
							"Subject:"+dm.getTitle()+"<BR>"+
							dm.getContent());
					deptMail.setTitle("Fe:"+dm.getTitle());
					this.receivePersonId=receiveId[0];
				}
			}
		}
		if (delIds!=null) {
			this.deptMailDel=this.getDeptMailDelService().get(delIds[0]);
			this.deptMail=deptMailDel.getDeptMail();
			this.deptMailReceive=deptMailDel.getDeptMailReceive();
			if (deptMail!=null) {
				if (types.equals("1")) {
					deptMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
							"To:"+deptMail.getReceivePerson()+"<BR>"+
							"Sent:"+deptMail.getRecordTime()+"<BR>"+
							"Subject:"+deptMail.getTitle()+"<BR>"+
							deptMail.getContent());
					deptMail.setTitle("Fe:"+deptMail.getTitle());
					this.receivePersonId=deptMail.getReceivePerosnId();
				}else{
					String[] receivePerson=this.deptMail.getReceivePerson().split(",");
					String[] receiveId=this.deptMail.getReceivePerosnId().split(",");
					deptMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
							"To:"+receivePerson[0]+"<BR>"+
							"Sent:"+deptMail.getRecordTime()+"<BR>"+
							"Subject:"+deptMail.getTitle()+"<BR>"+
							deptMail.getContent());
					deptMail.setTitle("Fe:"+deptMail.getTitle());
					this.receivePersonId=receiveId[0];
				}
			}
			if (deptMailReceive!=null) {
				DeptMail dm=this.getDeptMailService().getDeptMailById(deptMailReceive.getDeptMailId());
				if (dm!=null) {
					if (types.equals("1")) {
						deptMail=new DeptMail();
						deptMail.setContent("<BR>-----Original Message -----<BR>"+
								"Form:"+dm.getDeptMailNum().getMailAdress()+";<BR>"+
								"To:"+dm.getReceivePerson()+"<BR>"+
								"Sent:"+dm.getRecordTime()+"<BR>"+
								"Subject:"+dm.getTitle()+"<BR>"+
								dm.getContent());
						deptMail.setTitle("Fe:"+dm.getTitle());
						this.receivePersonId=dm.getReceivePerosnId();
					}else{
						String[] receivePerson=dm.getReceivePerson().split(",");
						String[] receiveId=dm.getReceivePerosnId().split(",");
						deptMail=new DeptMail();
						deptMail.setContent("<BR>-----Original Message -----<BR>"+
								"Form:"+dm.getDeptMailNum().getMailAdress()+";<BR>"+
								"To:"+receivePerson[0]+"<BR>"+
								"Sent:"+dm.getRecordTime()+"<BR>"+
								"Subject:"+dm.getTitle()+"<BR>"+
								dm.getContent());
						deptMail.setTitle("Fe:"+dm.getTitle());
						this.receivePersonId=receiveId[0];
					}
				}
			}
		}
		if(ids!=null){
			this.deptMail=this.getDeptMailService().get(ids[0]);	
			if (types.equals("1")) {
				deptMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
						"To:"+deptMail.getReceivePerson()+"<BR>"+
						"Sent:"+deptMail.getRecordTime()+"<BR>"+
						"Subject:"+deptMail.getTitle()+"<BR>"+
						deptMail.getContent());
				deptMail.setTitle("Fe:"+deptMail.getTitle());
				this.receivePersonId=deptMail.getReceivePerosnId();
			}else{
				String[] receivePerson=this.deptMail.getReceivePerson().split(",");
				String[] receiveId=this.deptMail.getReceivePerosnId().split(",");
				deptMail.setContent("<BR>-----Original Message -----<BR>"+
						"Form:"+deptMail.getDeptMailNum().getMailAdress()+";<BR>"+
						"To:"+receivePerson[0]+"<BR>"+
						"Sent:"+deptMail.getRecordTime()+"<BR>"+
						"Subject:"+deptMail.getTitle()+"<BR>"+
						deptMail.getContent());
				deptMail.setTitle("Fe:"+deptMail.getTitle());
				this.receivePersonId=receiveId[0];
			}
		}
		if (receiveIds!=null) {
			this.deptMailReceive=this.getDeptMailReceiveService().get(receiveIds[0]);
			DeptMail dm=this.getDeptMailService().getDeptMailById(deptMailReceive.getDeptMailId());
			if (dm!=null) {
				if (types.equals("1")) {
					deptMail=new DeptMail();
					deptMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+dm.getDeptMailNum().getMailAdress()+";<BR>"+
							"To:"+dm.getReceivePerson()+"<BR>"+
							"Sent:"+dm.getRecordTime()+"<BR>"+
							"Subject:"+dm.getTitle()+"<BR>"+
							dm.getContent());
					deptMail.setTitle("Fe:"+dm.getTitle());
					this.receivePersonId=dm.getReceivePerosnId();
				}else{
					String[] receivePerson=dm.getReceivePerson().split(",");
					String[] receiveId=dm.getReceivePerosnId().split(",");
					deptMail=new DeptMail();
					deptMail.setContent("<BR>-----Original Message -----<BR>"+
							"Form:"+dm.getDeptMailNum().getMailAdress()+";<BR>"+
							"To:"+receivePerson[0]+"<BR>"+
							"Sent:"+dm.getRecordTime()+"<BR>"+
							"Subject:"+dm.getTitle()+"<BR>"+
							dm.getContent());
					deptMail.setTitle("Fe:"+dm.getTitle());
					this.receivePersonId=receiveId[0];
				}
			}
		}
		return SUCCESS;
	}
	

	
	@Action(value = "removeSendDeptMail", results = {
			@Result(name = "success", type = "redirect", location = "listSendDeptMail"),
			@Result(name = "error", type = "chain", location = "listSendDeptMail") })
	public String removeSendDeptMail(){
		if (ids!=null) {
			this.deptMail=this.getDeptMailService().get(ids[0]);
		}
		deptMail.setType(3);
		this.getDeptMailService().submit(deptMail);
		DeptMailDel del=new DeptMailDel();
		del.setDeptMail(deptMail);
		this.getDeptMailDelService().save(del);
		return SUCCESS;
	}
	@Action(value = "removeSendDeptMails", results = {
			@Result(name = "success", type = "redirect", location = "listSendDeptMail"),
			@Result(name = "error", type = "chain", location = "listSendDeptMail") })
	public String removeSendDeptMails(){
		if (ids!=null) {
			this.deptMail=this.getDeptMailService().get(ids[0]);
		}
		this.getDeptMailService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "removeSendDeptMails1", results = {
			@Result(name = "success", type = "redirect", location = "listSendDeptMail"),
			@Result(name = "error", type = "chain", location = "listSendDeptMail") })
	public String removeSendDeptMails1(){
		if (ids!=null) {
			this.deptMail=this.getDeptMailService().get(ids[0]);
		}
		this.getDeptMailService().remove(deptMail);
		return SUCCESS;
	}
	@Action(value = "removeReceiveDeptMail", results = {
			@Result(name = "success", type = "redirect", location = "listReceiveDeptMail"),
			@Result(name = "error", type = "chain", location = "listReceiveDeptMail") })
	public String removeReceiveDeptMail(){
		if (receiveIds!=null) {
			this.deptMailReceive=this.getDeptMailReceiveService().get(receiveIds[0]);
		}
		deptMailReceive.setType(3);
		this.getDeptMailReceiveService().submit(deptMailReceive);
		DeptMailDel del=new DeptMailDel();
		del.setDeptMailReceive(deptMailReceive);
		this.getDeptMailDelService().save(del);
		return SUCCESS;
	}
	@Action(value = "removeReceiveDeptMails", results = {
			@Result(name = "success", type = "redirect", location = "listReceiveDeptMail"),
			@Result(name = "error", type = "chain", location = "listReceiveDeptMail") })
	public String removeReceiveDeptMails(){
		if (ids!=null) {
			this.deptMailReceive=this.getDeptMailReceiveService().get(ids[0]);
		}
		this.getDeptMailReceiveService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "removeDeptMailAll", results = {
			@Result(name = "success", type = "redirect", location = "listRemoveDeptMail"),
			@Result(name = "error", type = "chain", location = "listRemoveDeptMail") })
	public String removeDeptMailAll(){
		if (delIds!=null) {
			this.deptMailDel=this.getDeptMailDelService().get(delIds[0]);
		}
		if (deptMailDel.getDeptMail()!=null) {
			this.getDeptMailService().remove(deptMailDel.getDeptMail());
		}
		if (deptMailDel.getDeptMailReceive()!=null) {
			this.getDeptMailReceiveService().remove(deptMailDel.getDeptMailReceive());	
		}
		return SUCCESS;
	}
	@Action(value = "removeReceiveDeptMails1", results = {
			@Result(name = "success", type = "redirect", location = "listReceiveDeptMail"),
			@Result(name = "error", type = "chain", location = "listReceiveDeptMail") })
	public String removeReceiveDeptMails1(){
		this.getDeptMailReceiveService().remove(deptMailReceive);
		return SUCCESS;
	}
	@Action("viewDeptMailComment")
	public String viewDeptMailComment(){
		this.comment=this.deptMail.getDeptMailComment();
		return SUCCESS;
	}
	@Action("viewDeptMailReceiveComment")
	public String viewDeptMailReceiveComment(){
		this.comment=this.deptMailReceive.getDeptMailComment();
		return SUCCESS;
	}
	/**
	 * 部门发件箱评论
	 */
	@Action(value = "saveComment", results = {
			@Result(name = "success", type = "redirect", location = "viewDeptMailComment?deptMail=${deptMail.getId()}"),
			@Result(name = "error", type = "chain", location = "viewDeptMailComment?deptMail=${deptMail.getId()}") })
	public String saveComment(){
		deptMail.setCommentNum(deptMail.getCommentNum()+1);
		this.getDeptMailService().submit(deptMail);
		deptMailComment.setDeptMail(deptMail);
		this.getDeptMailCommentService().save(deptMailComment);
		return SUCCESS;
	}
	/**
	 * 部门收件箱评论
	 */
	@Action(value = "saveComments", results = {
			@Result(name = "success", type = "redirect", location = "viewDeptMailReceiveComment?deptMailReceive=${deptMailReceive.getId()}"),
			@Result(name = "error", type = "chain", location = "viewDeptMailReceiveComment?deptMailReceive=${deptMailReceive.getId()}") })
	public String saveComments(){
		deptMailReceive.setCommentNum(deptMailReceive.getCommentNum()+1);
		this.getDeptMailReceiveService().submit(deptMailReceive);
		deptMailComment.setDeptMailReceive(deptMailReceive);
		this.getDeptMailCommentService().save(deptMailComment);
		return SUCCESS;
	}
	@Action(value = "removeDeptComment", results = {
			@Result(name = "success", type = "redirect", location = "viewDeptMailComment?deptMail=${deptMailComment.getDeptMail().getId()}"),
			@Result(name = "error", type = "chain", location = "viewDeptMailComment?deptMail=${deptMailComment.getDeptMail().getId()}") })
	public String removeDeptComment(){
		this.getDeptMailCommentService().remove(deptMailComment);
		deptMailComment.setDeptMail(deptMailComment.getDeptMail());
		deptMailComment.getDeptMail().setCommentNum(deptMailComment.getDeptMail().getCommentNum()-1);
		this.getDeptMailService().submit(deptMailComment.getDeptMail());
		return SUCCESS;
	}
	@Action(value = "removeDeptComments", results = {
			@Result(name = "success", type = "redirect", location = "viewDeptMailReceiveComment?deptMailReceive=${deptMailComment.getDeptMailReceive().getId()}"),
			@Result(name = "error", type = "chain", location = "viewDeptMailReceiveComment?deptMailReceive=${deptMailComment.getDeptMailReceive().getId()}") })
	public String removeDeptComments(){
		this.getDeptMailCommentService().remove(deptMailComment);
		deptMailComment.setDeptMailReceive(deptMailComment.getDeptMailReceive());
		deptMailComment.getDeptMailReceive().setCommentNum(deptMailComment.getDeptMailReceive().getCommentNum()-1);
		this.getDeptMailReceiveService().submit(deptMailComment.getDeptMailReceive());
		return SUCCESS;
	}
	@Action(value="ajaxDeptMailByid",results = { @Result(name = "success", type = "json",
			params = {"root", "autograph" }) })
	public String ajaxDeptMailByid(){
		autograph=this.getDeptMailAutographService().getDeptMailAutographById(deptMailId);
		return SUCCESS;
	}
}
